﻿using System;
using System.Collections.Generic;
using EXT;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using System.Data;

namespace BanHangCshap.DanhMuc.gkhtdl63.Thuchi
{
    public partial class frmPhieuthu2 : Form
    {
        public frmPhieuthu2()
        {
            InitializeComponent();
        }

        Class.gkhtdl63.BLL.Phieuthu2BLL xgPhieuthu2BLL = new BanHangCshap.Class.gkhtdl63.BLL.Phieuthu2BLL();

        private void LoadPhieuthu()
        {
            gcPhieuthu2.DataSource = xgPhieuthu2BLL.Phieuthu2_Laydanhsach(dtpTungay.EditValue.ToDateTime(), dtpDenngay.EditValue.ToDateTime(), true);
        }

        private void frmKhachhang2_Load(object sender, EventArgs e)
        {
            dtpDenngay.EditValue = DateTime.Now;
            dtpTungay.EditValue = DateTime.Now;
            LoadPhieuthu();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet frm = new DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet(true, null, true);
            frm.Tailai += new MyEvent_LoadData(frm_Tailai);
            frm.ShowDialog();
        }

        void frm_Tailai(object sender, ELoadData e)
        {
            LoadPhieuthu();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                Int64 Id = gvPhieuthu2.GetFocusedRowCellValue("Id").ToInt64();
                if (Id > 0)
                {
                    DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet frm = new DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet(true, gvPhieuthu2.GetFocusedDataRow(), false);
                    frm.Tailai += new MyEvent_LoadData(frm_Tailai);
                    frm.ShowDialog();
                }

            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn phiếu cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gvKhachhang2_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Int64 Id = gvPhieuthu2.GetFocusedRowCellValue("Id").ToInt64();
                if (Id > 0)
                {
                    DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet frm = new DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet(true, gvPhieuthu2.GetFocusedDataRow(), false);
                    frm.Tailai += new MyEvent_LoadData(frm_Tailai);
                    frm.ShowDialog();
                }

            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn phiếu cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTailai_Click(object sender, EventArgs e)
        {
            DataTable xlDT = xgPhieuthu2BLL.Phieuthu2_In((long)gvPhieuthu2.GetFocusedRowCellValue("Id").ToFloat());
            if (xlDT!=null)
            {
                if (xlDT.Rows.Count>0)
                {
                    WaitDialogForm dg = new WaitDialogForm("Đang load dữ liệu...");
                    Report.gkhtdl63.frmMainRPTS frm = new BanHangCshap.Report.gkhtdl63.frmMainRPTS();
                    Report.gkhtdl63.MyRPT.Thuchi.rptThuchi rpt = new BanHangCshap.Report.gkhtdl63.MyRPT.Thuchi.rptThuchi();
                    rpt.DataSource = xlDT;
                    frm.SetReport(rpt);
                    dg.Close();
                    frm.ShowDialog();
                }
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            LoadPhieuthu();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                long Id = (long)gvPhieuthu2.GetFocusedRowCellValue("Id").ToFloat();
                if (Id > 0)
                {
                    if (xgPhieuthu2BLL.Phieuthu2_Xoa(Id))
                    {
                        LoadPhieuthu();
                    }
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Không thể xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn phiếu cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                Int64 Id = gvPhieuthu2.GetFocusedRowCellValue("Id").ToInt64();
                if (Id > 0)
                {
                    DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet frm = new DanhMuc.gkhtdl63.Thuchi.frmPhieuthu2Chitiet(true, gvPhieuthu2.GetFocusedDataRow(), false);
                    frm.Tailai += new MyEvent_LoadData(frm_Tailai);
                    frm.ShowDialog();
                }

            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn phiếu cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có chắc muốn xóa phiếu này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    long Id = (long)gvPhieuthu2.GetFocusedRowCellValue("Id").ToFloat();
                    if (Id > 0)
                    {
                        if (xgPhieuthu2BLL.Phieuthu2_Xoa(Id))
                        {
                            LoadPhieuthu();
                        }
                        else
                            DevExpress.XtraEditors.XtraMessageBox.Show("Không thể xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn phiếu cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            DataTable xlDT = xgPhieuthu2BLL.Phieuthu2_In((long)gvPhieuthu2.GetFocusedRowCellValue("Id").ToFloat());
            if (xlDT != null)
            {
                if (xlDT.Rows.Count > 0)
                {
                    WaitDialogForm dg = new WaitDialogForm("Đang load dữ liệu...");
                    Report.gkhtdl63.frmMainRPTS frm = new BanHangCshap.Report.gkhtdl63.frmMainRPTS();
                    Report.gkhtdl63.MyRPT.Thuchi.rptThuchi rpt = new BanHangCshap.Report.gkhtdl63.MyRPT.Thuchi.rptThuchi();
                    rpt.DataSource = xlDT;
                    frm.SetReport(rpt);
                    dg.Close();
                    frm.ShowDialog();
                }
            }
        }
    }
}
